<template>
  <div style="width: 100%; height: 100%">
    <div id="chart-line" ref="chart-line"></div>
  </div>
</template>

<script>
import * as echarts from "echarts/lib/echarts";
import { LineChart } from "echarts/charts";
import {
  TooltipComponent,
  LegendComponent,
  GridComponent,
  DataZoomComponent,
  TitleComponent,
  GraphicComponent,
} from "echarts/components";
echarts.use([
  LineChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  DataZoomComponent,
  TitleComponent,
  GraphicComponent,
]);
import { debounce } from "@/utils";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      chart: null,
    };
  },
  computed: {
    ...mapGetters(["autoResize"]),
  },
  watch: {
    autoResize: {
      handler: debounce(function () {
        this.chart.resize();
      }, 150),
    },
  },
  created() {
    this.$nextTick(() => {
      this.chart = echarts.init(this.$refs["chart-line"]);
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: "line",
          },
        ],
      });
    },
  },
};
</script>
<style scoped>
#chart-line {
  width: 100%;
  height: 100%;
}
</style>